package hongcaosp.app.android.contact.friends;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import hongcaosp.app.android.common.IListView;
import hongcaosp.app.android.login.UserToken;
import hongcaosp.app.android.modle.DataCallBack;
import hongcaosp.app.android.modle.bean.IUserDetail;
import hongcaosp.app.android.modle.bean.Message;
import hongcaosp.app.android.modle.bean.Pagebean;
import hongcaosp.app.android.modle.bean.UserInfo;
import hongcaosp.app.android.modle.impl.UserModleImpl;
import hongcaosp.app.android.modle.mi.UserModle;
import xlj.lib.android.base.component.BasePresenter;

public class FindFriendPresenter extends BasePresenter {
    private IListView<IUserDetail> listView;

    private UserModle userModle;

    private int number;
    private int total;

    public FindFriendPresenter(IListView<IUserDetail> listView) {
        this.listView = listView;
        userModle = new UserModleImpl();
    }

    public void freshData() {
        number = 1;
        userModle.recommendUser(UserToken.getDefault().getToken(), 20, number, new DataCallBack<Pagebean<UserInfo>>() {
            @Override
            public void onGetData(Pagebean<UserInfo> data) {
                super.onGetData(data);
                boolean loadMore = false;
                total = data.getList() == null ? 0 : data.getList().size();
                if (data.getTotal() > total) {
                    loadMore = true;
                }
                List<UserInfo> videos = data.getList();
                List<IUserDetail> videoList = new ArrayList<>();
                videoList.addAll(videos);
                listView.freshList(videoList, loadMore);
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                listView.freshList(null, false);
            }
        });
    }

    public void loadMore() {
        number++;
        userModle.recommendUser(UserToken.getDefault().getToken(), number, 20, new DataCallBack<Pagebean<UserInfo>>() {
            @Override
            public void onGetData(Pagebean<UserInfo> data) {
                super.onGetData(data);
                boolean loadMore = false;
                total += data.getList() == null ? 0 : data.getList().size();
                if (data.getTotal() > total) {
                    loadMore = true;
                }
                List<UserInfo> videos = data.getList();
                List<IUserDetail> videoList = new ArrayList<>();
                videoList.addAll(videos);
                listView.addList(videoList, loadMore);
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                listView.addList(null, false);
            }
        });
    }
}
